package com.jbm.jbmsupplier.utlis;

import android.content.Context;

import com.jbm.onehome.iface.PrivilegeListEntity;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

/**
 * Created by 家帮忙 on 2017/9/20.
 */

public class PtesonalPrivile {

public PtesonalPrivile(){}

//    public static PtesonalPrivile getInstant(){
//            return MyPtsonlPri.ptesonalPrivile;
//    }
//
//public static  class MyPtsonlPri{
//    private static PtesonalPrivile ptesonalPrivile=new PtesonalPrivile();
//
//}

    /**
     * 保存用户权限
     */

    private FileOutputStream opfile;
    private ObjectOutputStream obfile;
    public void setArrPteson(ArrayList<PrivilegeListEntity> ArrayListIEKey, Context context){
        try {
            opfile = context.openFileOutput("userPrson.txt", 0);
            obfile=new ObjectOutputStream(opfile);
            obfile.writeObject(ArrayListIEKey);
            obfile.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (obfile!=null){
                try {
                    obfile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }
    /**
     * 获取用户权限
     */
    private FileInputStream infile;
    private ObjectInputStream obinfile;
    private ArrayList<PrivilegeListEntity> arrPteson;
    public ArrayList<PrivilegeListEntity> getArrPteson(Context context){
        try {
            infile=context.openFileInput("userPrson.txt");
            obinfile=new ObjectInputStream(infile);
            if (obinfile!=null)
                arrPteson=(ArrayList<PrivilegeListEntity>) obinfile.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (obinfile!=null)
                    obinfile.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return arrPteson;

    }


/**
 * 获取用户的权限
 */
public String getPrson(Context context){
ArrayList<String > strarr=new ArrayList<>();
    ArrayList<PrivilegeListEntity> preson = getArrPteson(context);

if (preson!=null){
    for (int i = 0; i <preson.size() ; i++) {
        strarr.add(preson.get(i).getPriName());
    }
return  strarr.toString();
}else{
    return  "";
}


}




}
